package leetcode.code0434;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution434 implements IDebug {

//	public int countSegments(String s) {
//		int ans = 0;
//		boolean word = false;// 默认没有单词
//		for (int i = 0; i < s.length(); i++) {
//			if (s.charAt(i) != ' ') {
//				word = true;// 非空格找到一个单词
//				continue;
//			}
//			if (word) {// 空格前 有单词,计数器+1
//				ans++;
//				word = false;// 重新找单词
//				continue;
//			}
//		}
//		ans += word ? 1 : 0;// 末尾前有没有单词
//		return ans;
//	}

	public int countSegments(String s) {
		int segmentCount = 0;
		for (int i = 0; i < s.length(); i++) {
			if ((i == 0 || s.charAt(i - 1) == ' ') && s.charAt(i) != ' ') {
				segmentCount++;
			}
		}
		return segmentCount;
	}

	@Override
	public void debug1() {
		String str = "Hello, my name is John";
		int e = 5;
		int r = this.countSegments(str);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution434 so = new Solution434();
		so.debug1();
	}

}
